import javax.swing.*; 
import java.awt.event.*; 
import java.awt.GridLayout; 
import java.awt.BorderLayout; 

public class Calculator { 

	JPanel windowContent; 
	JTextField displayField; 
	JButton button1 = new JButton("1");
	JButton button2 = new JButton("2");
	JButton button3 = new JButton("3");
	JButton button4 = new JButton("4");
	JButton button5 = new JButton("5");
	JButton button6 = new JButton("6");
	JButton button7 = new JButton("7");
	JButton button8 = new JButton("8");
	JButton button9 = new JButton("9");
	JButton button0  = new JButton("0"); 
	JButton buttonPoint = new JButton(".");
	JButton buttonEqual = new JButton("=");
	JButton buttonPlus = new JButton("+");
	JButton buttonMinus = new JButton("-");
	JButton buttonDivide = new JButton("/");
	JButton buttonMultiply  = new JButton("*");
	JButton buttonCancel = new JButton("C");
	JPanel p1;
	JPanel p2; 
	JPanel p3;
	double value; 					
	double result;
	protected boolean clearField; 								
	protected String action;

	CalculatorBrains brain = new CalculatorBrains();
	
	Calculator() { 
		
		
		
		
		windowContent = new JPanel(); 
										

		BorderLayout bl = new BorderLayout();
		windowContent.setLayout(bl);

		displayField = new JTextField(30); 
											
		windowContent.add("North", displayField); 
													

		// Creating panel GridLayout

		p1 = new JPanel(); 
		GridLayout gl = new GridLayout(4, 3);
		p1.setLayout(gl); 
		// adding buttons

		p1.add(button1); 
		p1.add(button2);
		p1.add(button3);
		p1.add(button4);
		p1.add(button5);
		p1.add(button6);
		p1.add(button7);
		p1.add(button8);
		p1.add(button9);
		p1.add(button0);
		p1.add(buttonPoint);
		p1.add(buttonEqual);

		// panel > to the screen center

		windowContent.add("Center", p1); 

		// Creating panel for +.-./.*

		p2 = new JPanel();
		GridLayout gl2 = new GridLayout(4, 1);
		p2.setLayout(gl2);

		// add + - / * buttons to panel

		p2.add(buttonPlus);
		p2.add(buttonMinus); 
		p2.add(buttonDivide);
		p2.add(buttonMultiply);

		// postion for panel p2

		windowContent.add("East", p2); 

		//creating panel for "C" button 
		p3 = new JPanel(); 
		GridLayout gl3 = new GridLayout(1 ,1);
		p3.setLayout(gl3);
		
		//addind "C" button 
		
		p3.add(buttonCancel);
		
		//position for panel pe 
		
		windowContent.add("West", p3);
		
		
		
		// crating frame
		JFrame frame = new JFrame("Calculator"); 
		frame.setContentPane(windowContent);
		
		// setting up window size
		frame.pack(); 

		frame.setVisible(true); 

		// Creating listener and add it to each button
		
		
		
		 

		button0.addActionListener(new ActionListener() { 
															
			public void actionPerformed(ActionEvent e) {
				if (clearField) {
					displayField.setText(""); 
				}
				displayField.setText(displayField.getText() + "0");
			}
		}); 

		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (clearField) {
					displayField.setText("");
				}
				displayField.setText(displayField.getText() + "1");
			}
		});
		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (clearField) {
					displayField.setText("");
				}
				displayField.setText(displayField.getText() + "2");
			}
		});
		button3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (clearField) {
					displayField.setText("");
				}
				displayField.setText(displayField.getText() + "3");
			}
		});

		button4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (clearField) {
					displayField.setText("");
				}
				displayField.setText(displayField.getText() + "4");
			}
		});
		button5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (clearField) {
					displayField.setText("");
				}
				displayField.setText(displayField.getText() + "5");
			}
		});
		button6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (clearField) {
					displayField.setText("");
				}
				displayField.setText(displayField.getText() + "6");
			}
		});
		button7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (clearField) {
					displayField.setText("");
				}
				displayField.setText(displayField.getText() + "7");
			}
		});
		button8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (clearField) {
					displayField.setText("");
				}
				displayField.setText(displayField.getText() + "8");
			}
		});
		button9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (clearField) {
					displayField.setText("");
				}
				displayField.setText(displayField.getText() + "9");
			}
		});

		button0.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (clearField) {
					displayField.setText("");
				}
				displayField.setText(displayField.getText() + "0");
			}
		});

		buttonPoint.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (clearField) {
					displayField.setText("");
				}
				displayField.setText(displayField.getText() + ".");
			}
		});

		buttonCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (clearField) {
					displayField.setText("");
				}
				displayField.setText("");
			}
		
		});
		
		
		buttonPlus.addActionListener(new ActionListener() { 
					public void actionPerformed(ActionEvent e) { 
					brain.plus(getCurrentValue());
					 
					
					}
				});

		buttonMinus.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
					brain.minus(getCurrentValue());
					
				
			}
		});

		buttonDivide.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
					brain.divide(getCurrentValue());
					
				
			}
		});

		buttonMultiply.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
					brain.multiply(getCurrentValue());
					
				
			}
		});

		buttonEqual.addActionListener(new ActionListener() { 
					public void actionPerformed(ActionEvent e) { 
					setCurrentValue(brain.result());
					
					}
				});

	}
 
	
	private double getCurrentValue(){
		
		String currentValue = displayField.getText(); 
		double currentDoubleValue = Double.parseDouble(currentValue);
		return currentDoubleValue;
	}
	private  void setCurrentValue  (double currentValue) {
		
		String stringResult = String.valueOf(currentValue);
		displayField.setText(stringResult); 
			
	}
}